package utils

import (
	"os"
	"path/filepath"

	"gopkg.in/yaml.v3"
)

func Yaml[T any](filepath string) *T {
	content, e := os.ReadFile(getDefaultPath(filepath))
	if e != nil {
		panic(e)
	}

	var cfg T
	e = yaml.Unmarshal(content, &cfg)
	if e != nil {
		panic(e)
	}
	return &cfg
}

func getDefaultPath(url string) string {
	if url != "" {
		return url
	}

	execPath, err := os.Executable()
	if err != nil {
		panic(err)
	}

	return filepath.Join(filepath.Dir(execPath), "config.yaml")
}
